package master;

import master.Employer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmployerDaoImpl implements EmployerDao {
    @Override
    public List<Employer> getAllEmployers(Connection conn) throws SQLException {
        List<Employer> employers = new ArrayList<>();
        String sql = "SELECT empno, ename, job, deptno, password, role FROM employer";

        try (PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                Employer employer = new Employer();
                employer.setEmpno(rs.getInt("empno"));
                employer.setEname(rs.getString("ename"));
                employer.setJob(rs.getString("job"));
                employer.setDeptno(rs.getInt("deptno"));
                employer.setPassword(rs.getString("password"));
                employer.setRole(rs.getInt("role"));

                employers.add(employer);
            }
        }
        return employers;
    }

    @Override
    public int addEmployer(Connection conn, Employer employer) throws SQLException {
        String sql = "INSERT INTO employer(empno, ename, job, deptno, password, role) VALUES (?, ?, ?, ?, ?, ?)";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, employer.getEmpno());
            pstmt.setString(2, employer.getEname());
            pstmt.setString(3, employer.getJob());
            pstmt.setInt(4, employer.getDeptno());
            pstmt.setString(5, employer.getPassword());
            pstmt.setInt(6, employer.getRole());

            return pstmt.executeUpdate();
        }
    }
    /*public Employer login(Connection conn, Employer employer) throws Exception {
        Employer resultEmployer = null;
        String sql = "SELECT * FROM employer WHERE ename = ? AND password = ?";
        PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(sql);
        pstmt.setInt(1, employer.getEmpno());
        pstmt.setString(2, employer.getEname());
        pstmt.setString(3, employer.getJob());
        pstmt.setInt(4, employer.getDeptno());
        pstmt.setString(5, employer.getPassword());
        pstmt.setInt(6, employer.getRole());
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            resultEmployer = new Employer();
            resultEmployer.setEname(rs.getString("ename"));
        }
        return resultEmployer;
    }
    public static int addEmployer(Connection conn, Employer employer) throws Exception {
        // 检测注册用户是否存在
        String sql = "SELECT * FROM employer where ename = ?";
        PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(sql);
        pstmt.setString(1, employer.getEname());
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            return 2;
        }
        pstmt.close();
        rs.close();

        sql = "INSERT INTO employer (empno, ename,job, deptno, password, role) VALUES (?, ?, ?, ?, ?, ?)";
        PreparedStatement pstmt2 = (PreparedStatement) conn.prepareStatement(sql);
        pstmt.setInt(1, employer.getEmpno());
        pstmt.setString(2, employer.getEname());
        pstmt.setString(3, employer.getJob());
        pstmt.setInt(4, employer.getDeptno());
        pstmt.setString(5, employer.getPassword());
        pstmt.setInt(6, employer.getRole());
        return pstmt2.executeUpdate();
    }

    public ResultSet list(Connection conn, Employer employer) throws Exception {
        StringBuffer sb = new StringBuffer("select * from employer where role = 2");
        if (!ToolUtil.isEmpty(employer.getEname())) {
            sb.append(" and username like '%"+employer.getEname()+"%'");
        }
        PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(sb.toString());
        return pstmt.executeQuery();
    }

    public int update(Connection conn, Employer employer) throws Exception {
        String sql = "UPDATE employer SET password = ? WHERE ename = ?";
        PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(sql);
        pstmt.setString(1, employer.getEname());
        pstmt.setString(2, employer.getPassword());
        return pstmt.executeUpdate();
    }*/
}
